package com.example.demo.Controller;

import com.example.demo.Entity.Employee;
import com.example.demo.tools.Msg;
import com.example.demo.tools.ValidationHandle;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping(value = {"/emp"})
public class EmployeeController {


    @GetMapping(value = {"/sayhello"})
    public String sayhello(){
        return "hello";
    }

    @GetMapping(value = {"/testMsg"})
    public Msg testmsg(){
        Msg msg = new Msg().success();
        msg.putObject("list", "这是一个列表");
        return msg;
    }

    @GetMapping(value = {"/testjsr"})
    public Msg testJsr(@Valid Employee employee, BindingResult Result){
        Msg msg=null;
        if(Result.hasErrors()){
            List<String> errors = ValidationHandle.getErrors(Result);
            msg = Msg.fail();
            msg.putObject("errors", errors);
            msg.putObject("employee", employee);
            System.out.println(employee.toString());

        }else{
            msg=Msg.success();
            msg.putObject("message", "校验成功！！");
        }
        return  msg;
    }

}
